package service.sale;

import java.rmi.RemoteException;
import java.util.ArrayList;

import article.entities.ArticleVO;
import article.exceptions.OutOfStockIngredientsException;
import business.BusinessFacade;
import business.sale.interfaces.SaleMgt;
import sale.entities.FacturaVO;
import sale.entities.OrderVO;
import sale.entities.SaleVO;
import sale.exceptions.EmptyOrderException;
import sale.exceptions.EmptySaleExcpetion;
import sale.exceptions.NoSalesException;
import sale.interfaces.SaleRemoteMgt;

public class SaleRemoteMgr implements SaleRemoteMgt {

	/**
	 * Singleton implementation begin
	 */
	private static SaleRemoteMgr instance = null;

	private SaleRemoteMgr() {
	}

	public static SaleRemoteMgr getInstance() {
		if (instance == null)
			instance = new SaleRemoteMgr();
		return instance;
	}

	/**
	 * Singleton implementation end
	 * 
	 * @throws NoSalesException
	 */

	@Override
	public SaleVO[] getOpenSales() throws RemoteException, NoSalesException {
		SaleMgt sMgt = BusinessFacade.getInstance().getSaleMgt();
		return sMgt.getOpenSales();
	}

	@Override
	public void newSale(OrderVO ord, int tableKey, int ci) throws RemoteException,
			EmptyOrderException, OutOfStockIngredientsException {
		SaleMgt sMgt = BusinessFacade.getInstance().getSaleMgt();
		sMgt.newSale(ord, tableKey,ci);
	}

	@Override
	public void addOrder(OrderVO ord, int tableKey) throws RemoteException,
			EmptySaleExcpetion, OutOfStockIngredientsException {
		SaleMgt sMgt = BusinessFacade.getInstance().getSaleMgt();
		sMgt.addOrder(ord, tableKey);
	
	}

	@Override
	public FacturaVO close(SaleVO sale) throws RemoteException,
			EmptySaleExcpetion {
		SaleMgt sMgt = BusinessFacade.getInstance().getSaleMgt();
		return sMgt.close(sale);
	}
	
	@Override
	public void commit(FacturaVO fact) throws RemoteException {
		BusinessFacade.getInstance().getSaleMgt().commit(fact);		
	}
	
	@Override
	public void deleteArticles(SaleVO sale, ArrayList<ArticleVO> arts)
			throws RemoteException {
		SaleMgt sMgt = BusinessFacade.getInstance().getSaleMgt();
		try {
			sMgt.deleteArticles(sale, arts);
		} catch (Exception e) {
		}
	}

	@Override
	public boolean hasOpenSale(int tableKey) throws RemoteException {
		SaleMgt sMgt = BusinessFacade.getInstance().getSaleMgt();
		return sMgt.hasOpenSale(tableKey);
	}


	@Override
	public SaleVO[] getOpenTakeoOutSales() throws RemoteException,
			NoSalesException {
		SaleMgt sMgt = BusinessFacade.getInstance().getSaleMgt();
		return sMgt.getOpenTakeoOutSales();
	}

	@Override
	public SaleVO[] getOpenDeliverySales() throws RemoteException,
			NoSalesException {
		SaleMgt sMgt = BusinessFacade.getInstance().getSaleMgt();
		return sMgt.getOpenDeliverySales();
	}

	@Override
	public SaleVO getSale(int tableKey) throws RemoteException {
		SaleMgt sMgt = BusinessFacade.getInstance().getSaleMgt();
		return sMgt.getSale(tableKey);
	}

	@Override
	public int[] getOpenTables() throws RemoteException {
		SaleMgt sMgt = BusinessFacade.getInstance().getSaleMgt();
		return sMgt.getOpenTables();
	}

	@Override
	public OrderVO[] getOpenOrders() throws RemoteException {		
		return BusinessFacade.getInstance().getSaleMgt().getOpenOrders();
	}
}
